import "reflect-metadata";
import { createConnection } from "typeorm";
import { Server } from "../entity/Server";
import { Request, Response } from "express";

let moment = require('moment-timezone');

const connR = createConnection("login");

export function serverRoute(app: any) {

    app.put('/api/server', function (req: Request, res: Response) {

        connR.then(async connection => {

            let server = new Server();
            server.name = req.body.name;
            server.address = req.body.address;
            server.state = req.body.state;
            server.address = req.body.address;
            server.created_at = moment().format();
            server.updated_at = server.created_at;
            server.open_time = req.body.open_time;

            await connection.getRepository(Server).save(server);

            res.send({ success: true })

        }).catch(error => console.log(error));
    })
   
    app.post('/api/server/:id', function (req: Request, res: Response) {

        connR.then(async connection => {


            let repository = connection.getRepository(Server);
            let server = await repository.findOne(req.params.id);
            server.name = req.body.name;
            server.address = req.body.address;
            server.state = req.body.state;
            server.address = req.body.address;
            server.open_time=req.body.open_time;
            server.updated_at = moment().format();
            
            await repository.save(server);

            res.send({ success: true })

        }).catch(error => res.send({ success: false, msg: error }));
    })

    app.get('/api/server/:id', function (req: Request, res: Response) {

        connR.then(async connection => {

            let data = await connection.getRepository(Server).findOne(req.params.id);
            data.open_time = moment(data.open_time).format();
            res.send(data)

        }).catch(error => console.log(error));
    })

    app.get('/api/servers', function (req: Request, res: Response) {

        const pageNum = 20

        let page = req.query.page
        if (!page) { page = 1 }

        connR.then(async connection => {

            let data = await connection.getRepository(Server).find({ order: { open_time: 'DESC' }, skip: (page - 1) * pageNum, take: pageNum });

            data.forEach(element => {

                element.open_time = moment(element.open_time).format();
            });

            res.send(data)

        }).catch(error => console.log(error));
    })

    app.get('/server', function (req: Request, res: Response) {


        connR.then(async connection => {


            let data = await connection.getRepository(Server).createQueryBuilder().select("COUNT(*) AS c").getRawOne();
            res.render('server/server', { total: data.c })

        }).catch(error => console.log(error));

    })

    app.delete('/api/server/:id', function (req: Request, res: Response) {

        connR.then(async connection => {

            await connection.getRepository(Server).delete(req.params.id);
            res.send({ success: true })

        }).catch(error => console.log(error));
    })
}




